package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.pojo.TbGoods;
import com.dongdong.service.GoodsService;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsContrller")
public class GoodsController {

    @Reference
    private GoodsService goodsService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("select")
    @ResponseBody
    public Result select(String goodsName){
        List<TbGoods> goods = goodsService.selectAll(goodsName);
        return Result.builResult(ResultEnum.SUCCESS).setResout(goods);
    }

    @RequestMapping("pass")
    @ResponseBody
    public Result Pass(Long id){
        try {
            rocketMQTemplate.convertAndSend("AUDIT",id.toString());
            goodsService.pass(id);
                return Result.builResult(ResultEnum.SUCCESS);
        }catch (Exception r){
            r.printStackTrace();
            return Result.builResult(ResultEnum.ERROR);
        }

    }

    @RequestMapping("overrule")
    @ResponseBody
    public Result overrule(Long id){
        boolean b = goodsService.overrule(id);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }
}
